package com.ww.java7.tryauto;

/**
 * @author wanggw
 * @Date 2023年02月28 23:58
 */
public class AutoCloseResource {

    /**
     * 输出
     * mysql已连接
     * OracleDatabase已连接
     * OracleDatabase已关闭
     * mysql已关闭
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        try (Mysql mysql = new Mysql();
            OracleDatabase oracleDatabase = new OracleDatabase()) {
            mysql.conn();
            oracleDatabase.conn();
        }
    }
}

class Mysql implements AutoCloseable {

    @Override
    public void close() throws Exception {
        System.out.println("mysql已关闭");
    }

    public void conn() {
        System.out.println("mysql已连接");
    }
}

class OracleDatabase implements AutoCloseable {

    @Override
    public void close() throws Exception {
        System.out.println("OracleDatabase已关闭");
    }

    public void conn() {
        System.out.println("OracleDatabase已连接");
    }
}
